﻿using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using SharpDX;
using SharpDX.Multimedia;
using SharpDX.XAudio2;
using ScheduledXAudio2.Structures;

namespace ScheduledXAudio2
{
    public abstract class PatchHost : DeviceBase, IDisposable
    {
        #region fields
        protected Clock Clock;
        protected VoiceSendDescriptor vsd2;  
        protected SubmixVoice sound_submixvoice;
        protected SubmixVoice sound_submixvoice2;  
        internal SimpleUnit Unit;
        #endregion Fields

        public SimpleUnit UNIT { get { return Unit; } }
        public SubmixVoice SubMixer
        {
            get
            {
                if (sound_submixvoice == null)
                {
                    sound_submixvoice = new SubmixVoice(Engine.DeviceAccessor, 2, 44100, VoiceSendFlags.UseFilter, 0);
                    return sound_submixvoice;
                }
                return sound_submixvoice;
            }
        }
        public SubmixVoice SubMixer2
        {
            get
            {
                if (sound_submixvoice2 == null)
                {
                    sound_submixvoice2 = new SubmixVoice(Engine.DeviceAccessor, 2, 44100, VoiceSendFlags.UseFilter, 0);
                    return sound_submixvoice2;
                }
                return sound_submixvoice2;
            }
        }      

        #region Constructors
        protected PatchHost()
            : base("null")
        {
            SetupSubmixers();
        }
        protected PatchHost(string path): base(path)
        {
            SetupSubmixers();
            Load(path);
        }
        protected PatchHost(System.IO.Stream stream)
            : base(stream)
        {
            SetupSubmixers();
            Load(stream);
        }

#endregion

        #region Methods

        public virtual bool Load(string soundName) { return true; }
        public virtual bool Load(System.IO.Stream stream) { return true; }
        public virtual void Unload() { }
        protected void SetupSubmixers()
        {
            Engine.AddHost(this);
            sound_submixvoice = new SubmixVoice(Engine.DeviceAccessor, 2, 44100, VoiceSendFlags.UseFilter, 0);
            sound_submixvoice2 = new SubmixVoice(Engine.DeviceAccessor, 2, 44100, VoiceSendFlags.UseFilter, 1);
           
            vsd2 = new VoiceSendDescriptor();
            vsd2.OutputVoice = sound_submixvoice2;            

            sound_submixvoice.SetOutputVoices(vsd2);
        }
        #endregion Methods

        #region IDisposable

        public virtual void Dispose()
        {
            //graphingEffect.Dispose();
        }

        #endregion
    }
}
